/*
 * PurchaseOrderAction.java
 *
 * Created on June 8, 2002, 10:23 PM
 */

package com.scalar.chiptrack.operations.dataentry.actions;

import java.util.ArrayList;
import java.util.Iterator;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.struts.action.ActionForm;
import org.apache.struts.action.ActionForward;
import org.apache.struts.action.ActionMapping;

import com.scalar.chiptrack.ChipTrackAction;
import com.scalar.chiptrack.db.DBTransaction;
import com.scalar.chiptrack.operations.dataentry.forms.LotCreationForm;
import com.scalar.chiptrack.operations.process.VendorLocation;
import com.scalar.chiptrack.operations.process.dao.LotDAO;
import com.scalar.chiptrack.operations.process.dao.PartNumber;
import com.scalar.chiptrack.operations.process.dao.ProductDao;
import com.scalar.chiptrack.operations.vendor.dao.VendorDAO;

/**
 *
 * @author  Mallikarjun
 * @version 1.0
 */
public class LotCreationAction extends ChipTrackAction{
    
    public ActionForward execute (ActionMapping mapping,
                                  ActionForm form,
                                  HttpServletRequest request,
                                  HttpServletResponse response) 
                                  throws Exception {
    
        String action = request.getParameter("action");
		if(action==null)
        action="";
		LotCreationForm lform = (LotCreationForm)form;
		ArrayList partNumberList = ProductDao.getPartNumber("FAB");
        Iterator it = partNumberList.iterator();
        ArrayList pList = new ArrayList();
        	
        while(it.hasNext()){
        	PartNumber pn = (PartNumber)it.next();
        	pList.add(new org.apache.struts.util.LabelValueBean(pn.getPartNumber(), pn.getProcessStepId()+""));
        }
         	
        ArrayList fabLocationList = null;
		ArrayList vendorLocationList =null;

		if(action.equalsIgnoreCase("product") || action.equalsIgnoreCase("save")){
        String processStepID = lform.getProductName();
        fabLocationList= VendorDAO.getSelectedLocations(Integer.parseInt(processStepID));
		vendorLocationList = new ArrayList();
		it = null;
		it = fabLocationList.iterator();
			
		while(it.hasNext()){
			VendorLocation vendorLocation=(VendorLocation)it.next();
			vendorLocationList.add(new org.apache.struts.util.LabelValueBean(vendorLocation.getLocationName(), vendorLocation.getLocationID()+""));
		}
		}
		request.setAttribute("lotCreationForm",lform);
		request.setAttribute("action",action);
		request.setAttribute("fabLocationList",vendorLocationList);
		request.setAttribute("pList", pList);
		
			
        if ((action != null) && (action.equalsIgnoreCase("save"))) {
            
            DBTransaction dbTransaction = null;
            boolean success = false;
                        
            try{
                dbTransaction = DBTransaction.getInstance();
                dbTransaction.begin();
                
                String processStepID = lform.getProductName();
                String locationID = lform.getLocationName();
                String lotNumber = lform.getLotNo();
                String lotID = LotDAO.getLotId(lotNumber, locationID, processStepID);
                
                //System.out.println("ProcessStepID = "+ processStepID);
                //System.out.println("locationID = "+locationID);
                //System.out.println("Existing Lot ID = "+ lotID);
                if(lotID.equals("")){
                
                	String lotid = "";
                	lotid = LotDAO.createLot(dbTransaction, 
                					 		 processStepID,
                					 		 locationID,
                					 		 lotNumber,
                					 		 lform.getPurchaseOrderNo(),
                					 		 lform.getVendorLotNo(),
                					 		 lform.getDateIn(),
                					 		 lform.getQuantity(),
                					 		 lform.getCreatedBy()
                					 		);
					//System.out.println("LOT ID = "+lotid);	
                }else{
                	request.setAttribute("errorMessage", "Lot Number Exists");
					request.setAttribute("fabLocationList",vendorLocationList);
		        	request.setAttribute("pList", pList);
		        	
		        	return mapping.findForward("success");
                }
                
                success = true;
                
            }catch(Exception e){
                e.printStackTrace();
            }finally{
                
                if(success == true){
                    dbTransaction.commit();
                }else{
                    dbTransaction.rollback();
                }    
            }
            
        }else{
			
			request.setAttribute("fabLocationList",vendorLocationList);
        	request.setAttribute("pList", pList);
        	
        	return mapping.findForward("success");
        }
        
		request.setAttribute("errorMessage", "Saved successfully");
		return mapping.findForward("success");
        
    }
}
